package com.chs.recoginition.vo.respVo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author CHS
 * @Date 2022/8/6 22:35
 * @Version 1.0
 **/
@Data
public class UserRespVo implements UserDetails {
    /**
     * 用户ID
     */
    private Integer userId;

    /**
     * 头像路径
     */
    private String headImageUrl;

    /**
     * 电子邮箱
     */
    private String email;

    /**
     * 权限
     */
    private Integer userType;

    /**
     * token值
     */
    private String token;

    /**
     * 昵称
     */
    private String nickname;


    private String username;
    @JsonIgnore
    private String password;
    /**
     * spring security 权限
     */
    private List<GrantedAuthority> authorityList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if (userType == 0) {
            authorityList = new ArrayList<>();
            //this.roles.forEach(role -> authorityList.add(new SimpleGrantedAuthority(role.getCode())));
            //this.resources.forEach(resource -> authorityList.add(new SimpleGrantedAuthority(resource.getCode())));
            authorityList.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        if(userType == 1) {
            authorityList = new ArrayList<>();
            authorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }
        return authorityList;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
